#classe pour recuprer les information sur internet

#Utilisation su site : http://fr.bookbutler.com/do/bookSearch?showMore=false&shipTo=fr&amountIn=eur&zip=&sortBy=salesrank&pageNr=1&searchFor=9782847894493&searchBy=isbn&searchIn=fr
import urllib
from BeautifulSoup import BeautifulSoup
from biblio import *

class InformationParser():
    """Parser les information utile depuis le site http://www.amazon.fr/"""
    
    def __init__(self,isbn=-1):
        self.__traitement(isbn)
    
    def __recupererHTML(self,isbn):
        isbn = ISBN(isbn)
        if isbn!=ISBNvide:
            URLhtml = "http://www.amazon.fr/dp/%s/?ie=UTF8" % (isbn)
            #print URLhtml
            html=' '.join(urllib.urlopen(URLhtml).readlines()) #TODO: pas de close...
            #html = unicode(html,'UTF-8')
            return (html,True)
        else:
            return ("",False)
    
    def __traitement(self,isbn):
        html, nonErreur = self.__recupererHTML(isbn)
        if nonErreur:
            self.__parser = BeautifulSoup(html,fromEncoding='UTF-8') #TODO:verf
            tmp = BeautifulSoup()
            tmp2 = BeautifulSoup()
            for i in  self.__parser('span'):
                try:
                    i['id']
                except:
                    pass
                else:
                    if i['id']=='btAsinTitle':
                            #print i.__dict__
                            print i.string
                            string = i.previous.parent
                            string2 = ""
                            for i in string:
                                string2 = string2 + str(i)
                            string2 = unicode(string2,'UTF-8')
                            tmp.feed(string2)
                            for i in tmp('a'): #Toute les balises a
                                print i.string
if __name__== "__main__":
    InformationParser("2845997809")
